﻿using MaiPlanServer.Dto;
using MaiPlanServer.IRepository;
using MaiPlanServer.IServices.IEquipmentServices;
using MaiPlanServer.Model;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace MaiPlanServer.Services.EquipmentServices
{
    public class DeviceTypeServices : IDeviceTypeServices
    {
        public DeviceTypeServices(IBaseRepository BaseConn)
        {
            this.BaseConn = BaseConn;
        }

        public IBaseRepository BaseConn { get; }
        public async Task<bool> AddAsync(dv_machinery_type model)
        {
            var result = await BaseConn.InsertAsync(model);
            return result;
        }
        public async Task<bool> UpdateAsync(dv_machinery_type model)
        {
            var result = await BaseConn.UpdateAsync(model, "machinery_type_id");
            return result;
        }

        public async Task<bool> DellAllAsync(string[] Id)
        {
            var result = await BaseConn.DeleteAsync(@"with tab as (
	                select * from  dv_machinery_type where  machinery_type_id=@Id
	                union all 
	                select child.* from  dv_machinery_type  child join tab parent on parent.machinery_type_id = child.parent_type_id
                )
                delete dv_machinery_type where machinery_type_id in (select machinery_type_id from tab)", new { Id = Id });
            return result;
        }

        public  Task<bool> DellAsync(string Id)
        {
            var result = BaseConn.DeleteAsync(@"with tab as (
	                select * from  dv_machinery_type where  machinery_type_id=@Id
	                union all 
	                select child.* from  dv_machinery_type  child join tab parent on parent.machinery_type_id = child.parent_type_id
                )
                delete dv_machinery_type where machinery_type_id in (select machinery_type_id from tab)", new { Id = Id });
            return result;
        }

        public async Task<int> GetCountAsync(string mtName)
        {
            string sql = @"select count(1) from dv_machinery_type where parent_type_id is null and machinery_type_name like @mtName ";

            var count = await BaseConn.GetRecoreCountAsync(sql, new { mtName = "%" + mtName + "%" });

            return count;
        }

        public async Task<IEnumerable<dv_machinery_typeDto>> GetJsonAsync(int PageIndex, int PageSize, string mtName, string enable_flag)
        {
            string sql = @"with tab as(
		                    select * from dv_machinery_type  where parent_type_id is null ";

            if (!string.IsNullOrEmpty(mtName))
            {
                sql += " and machinery_type_name like @mtName ";
            }
            if (!string.IsNullOrEmpty(enable_flag))
            {
                sql += " and enable_flag =@enable_flag ";
            }
            

            sql += " order by create_time desc offset @PageSize*(@PageIndex-1) rows fetch next @PageSize row only ";

            sql += @" union all select child.*from dv_machinery_type child join tab on tab.machinery_type_id = child.parent_type_id
	                 ) select* from tab";

            var list = await BaseConn.QueryAsync<dv_machinery_typeDto>(sql, new { PageIndex, PageSize, mtName = "%" + mtName + "%", enable_flag=  enable_flag });

            return list;
        }


        public async Task<string> GetCodeasync(string mtName)
        {
            var CodeNo = DateTime.Now.ToString("yyyy-MM-dd");
            string sql = @"select top(1)machinery_type_code from dv_machinery_type where machinery_type_code like @CodeNo  order by create_time desc ";
            var machinery_type_code = await BaseConn.QueryFirstOrDefaultAsync<string>(sql, new { CodeNo= "%"+CodeNo +"-%" });

            var Prefixs = await BaseConn.QueryFirstOrDefaultAsync<string>("select RULE_CODE from SYS_AUTO_CODE_RULE where RULE_NAME = @mtName", new { mtName });

            if (string.IsNullOrEmpty(machinery_type_code))
            {
                CodeNo = $"{Prefixs}-{CodeNo}-001";
            }
            else
            {
                var numberStr = machinery_type_code.Substring(machinery_type_code.LastIndexOf('-')+1);

                int num = 0;
                if(int.TryParse(numberStr,out num))
                {
                    num += 1;
                    if (num < 10)
                    {
                        CodeNo = $"{Prefixs}-{CodeNo}-00{num}";
                    }
                    else if (num < 100)
                    {
                        CodeNo = $"{Prefixs}-{CodeNo}-0{num}";
                    }
                    else
                    {
                        CodeNo = $"{Prefixs}-{CodeNo}-{num}";
                    }
                }
            }

            return CodeNo;
        }

    }
}
